package com.bdqn.t350.ch11.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t350.ch11.bean.Role;
import com.bdqn.t350.ch11.bean.User;
import com.bdqn.t350.ch11.service.RoleService;
import com.bdqn.t350.ch11.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/user")
@Controller
public class UserController {

    @Resource
    UserService userService;

    @Resource
    RoleService roleService;

    /**
     * 普通列表，没有实现分页
     *
     * @param usrName
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public Object getList(@RequestParam(name = "usrName", defaultValue = "") String usrName,
                          @RequestParam(name = "roleId", defaultValue = "") String roleId, Model model) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("usr_name", usrName);
        List<User> list = userService.selectList(queryWrapper);
        List<Role> roleList = roleService.selectList(null, new Role());
        model.addAttribute("list", list);
        model.addAttribute("roleList", roleList);
        model.addAttribute("roleId", roleId);
        model.addAttribute("usrName", usrName);
        return "user/list";
    }

    /**
     * mybatisPlus 分页功能
     *
     * @param usrName
     * @param index
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/selectPage")
    public Object selectPage(@RequestParam(name = "usrName", defaultValue = "") String usrName,
                             @RequestParam(name = "pageIndex", defaultValue = "1") int index,
                             @RequestParam(name = "roleId", defaultValue = "") String roleId,
                             Model model) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("usr_name", usrName);
        if (!roleId.equals("")) {
            queryWrapper.eq("role_id", roleId);
        }
        IPage<User> page = new Page<>();
        page.setCurrent(index);
        userService.selectPage(page, queryWrapper);
        List<Role> roleList = roleService.selectList(null, new Role());
        model.addAttribute("page", page);
        model.addAttribute("roleList", roleList);
        model.addAttribute("usrName", usrName);
        return "user/list";
    }


    /**
     * 通过分页拦截器实现多表自定义条件查询分页功能
     *
     * @param usrName
     * @param index
     * @param roleId
     * @param model
     * @return
     */
    @RequestMapping("/listPageAssociation")
    public Object listPageAssociation(@RequestParam(name = "usrName", defaultValue = "") String usrName,
                                      @RequestParam(name = "pageIndex", defaultValue = "1") int index,
                                      @RequestParam(name = "roleId", defaultValue = "0") Long roleId,
                                      Model model) {
        Page<User> page = new Page<>();
        page.setCurrent(index);
        User user = new User();
        user.setUsrName(usrName);
        user.setUsrRoleId(roleId);
        userService.listPageAssociation(page, user);
        List<Role> roleList = roleService.selectList(null, new Role());
        model.addAttribute("page", page);
        model.addAttribute("roleList", roleList);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        return "user/list";
    }


    /**
     * 根据ID删除
     *
     * @param usrId
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteById")
    public Object deleteById(int usrId) {
        return userService.deleteById(usrId);
    }

    /**
     * 转向导添加页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/toadd")
    public String toadd(Model model) {
        //List<Role> roleList = roleService.selectList(new Role());
        //model.addAttribute("roles", roleList);
        return "user/add";
    }

    /**
     * 保存
     *
     * @param user
     * @return
     */
    @PostMapping("/save")
    public String save(User user) {
        int count = userService.insert(user);
        if (count > 0) {
            return "redirect:/user/listPageAssociation";
        } else {
            return "user/add";
        }
    }

    /**
     * 根据id查看
     *
     * @param usrId
     * @param model
     * @return
     */
    @RequestMapping("/selectById")
    public String selectById(int usrId, Model model) {
        User user = userService.selectById(usrId);
        model.addAttribute("user", user);
        return "user/view";
    }

    /**
     * 根据ID修改
     *
     * @param user
     * @return
     */
    @PostMapping("/edit")
    public Object edit(User user) {
        int count = userService.updateById(user);
        if (count > 0) {
            return "redirect:/user/listPageAssociation";
        } else {
            return "redirect:/user/listPageAssociation";
        }
    }


    @ResponseBody
    @GetMapping("/redistest/{username}")
    public Object redistest(@PathVariable("username") String username) {
        System.out.println("username");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_name", "czkt");
        List<User> list = userService.selectList(queryWrapper);
        return list;
    }
}
